package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.system.domain.DrugWareHousingDetail;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingDetailVO;
import com.zgjkhis.modules.system.service.dto.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yzq
 * @Description 药品入库清单
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface DrugWareHousingDetailMapper extends CoreMapper<DrugWareHousingDetail> {


    @Select("<script>select t.id,drug_id,t.drug_name,scheduled_quantity,t.stock,t.min_stock,t.sale_price ,t.cost_price,term_of_validity,batch_no,t.ware_housing_no,t.drug_spec,t.package_unit,\n" +
            "t.dose_unit,t.pharmacy_id ,t.left_count,t.min_left_count,t.self_price,t.red_pun,equivalent,t.coefficient_name,t.coefficient,t.commodity_price,t.commodity_self_price,\n" +
            "actual_quantity,d.drug_type,IFNULL(d.total_dose,1) as total_dose, dw.supplier_name as drugManufactor from his_drug_warehousing_detail t  \n" +
            "left join his_drug_warehousing dw on t.ware_housing_no=dw.ware_housing_no\n" +
            "left join his_drug d on t.drug_id=d.id\n" +
            "where 1=1 and t.pharmacy_id=#{drug.pharmacyId}\n" +
            "<if test=\"drug.drugType !=null and drug.drugType != '' and drug.drugType!=7\">\n" +
            "\t and d.drug_type = #{drug.drugType}\n" +
            "</if>\n" +
            "<if test=\"drug.drugType !=null and drug.drugType != '' and drug.drugType ==7\">\n" +
            "\t and d.drug_type  in (3,4)\n" +
            "</if>\n" +
            "and dw.status=3 and t.left_count>0\n" +
            "<if test=\"drug.drugName !=null and drug.drugName!=''\">\n" +
            "and (d.drug_name LIKE concat('%', #{drug.drugName}, '%') or d.pinyin  LIKE concat('%', #{drug.drugName}, '%'))"+
            "</if>\n</script>")
    List<DrugWareHousingDetailDto> findAllWareDetailByPageable(@Param("drug") DrugQueryCriteria criteria);

    @Select("<script>select t.id,t.drug_id,t.drug_name,d.drug_no,d.drug_manufactor,d.drug_spec," +
            "t.cost_price,t.self_price,t.red_pun,d.drug_type,t.sale_price," +
            "dw.ware_housing_type,t.batch_no,t.term_of_validity,t.left_count,t.package_unit from his_drug_warehousing_detail t \n" +
            "            left join his_drug_warehousing dw on t.ware_housing_no=dw.ware_housing_no\n" +
            "            left join his_drug d on t.drug_id=d.id\n" +
            "            where 1=1 and t.pharmacy_id=#{drug.pharmacyId}  and dw.status=3 and t.left_count>0\n" +
            "\t<if test=\"drug.flag==1\">\n" +
            "\t\tand t.term_of_validity &lt; CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==3\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 1 WEEK) and t.term_of_validity  &gt;= CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==2\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 1 MONTH) and t.term_of_validity  &gt;= CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==4\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 3 MONTH) and t.term_of_validity  &gt;= CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==5\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 6 MONTH) and t.term_of_validity  &gt;= CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==6\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 1 YEAR) and t.term_of_validity  &gt;= CURDATE() \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.flag==7\">\n" +
            "\t\t and t.term_of_validity &lt; DATE_ADD(CURDATE(), INTERVAL 6 MONTH) \n" +
            "\t</if>\n" +
            "\t<if test=\"drug.drugName != null \">\t\n" +
            "            and (d.drug_name LIKE concat('%', #{drug.drugName}, '%') or d.pinyin  LIKE concat('%', #{drug.drugName}, '%'))\n" +
            "\t</if>\n" +
            "\t<if test=\"drug.drugType != null and drug.drugType != ''\">\n" +
            "\t and d.drug_type=#{drug.drugType}\n" +
            "\t</if>\n" +
            "\t<if test=\"drug.drugType == null or drug.drugType == ''\">\n" +
            "\tand d.drug_type in (2,3,4,5,6)\n" +
            "\t</if>\n" +
            "\t\n</script>")
    List<DrugRemainDto> findRemainByPage(@Param("drug")DrugRemainQueryCriteria criteria);

    @Select("<script>select distinct t.*,dw.ware_housing_type,dr.drug_type,dw.supplier_name,dw.warehousing_time from his_drug_warehousing_detail t \n" +
            "left join his_drug_warehousing dw on t.ware_housing_no=dw.ware_housing_no\n" +
            "left join his_drug dr on t.drug_id=dr.id \n" +
            "where dw.status=3 and t.drug_id=#{drug.drugId} and t.pharmacy_id=#{drug.pharmacyId}" +
            "<if test=\"drug.type!=null\">\n" +
            "and dw.ware_housing_type=#{drug.type}\n" +
            "</if>" +
            "<if test=\"drug.supplierId!=null\">\n" +
            "and dw.supplier_id=#{drug.supplierId}\n" +
            "</if>" +
            "<if test=\"drug.startDate !=null \">\n" +
            " and dw.warehousing_time  &gt; #{drug.startDate}\n" +
            "</if>\n" +
            "<if test=\"drug.endDate !=null \">\n" +
            " and dw.warehousing_time  &lt; str_to_date(DATE_ADD(#{drug.endDate},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>\n" +
            "</script>")
    List<DrugWareHousingDetailDto> findAllByPageable(@Param("drug")DrugQueryCriteria criteria);

    @Select("<script>select t.id,t.drug_name,t.drug_id,t.drug_spec,t.drug_manufactor,t.batch_no,t.ware_housing_no,dw.warehousing_time,t.equivalent equivalent\n" +
            ",t.term_of_validity,d.drug_type,d.package_unit,d.dose_unit,t.stock,t.left_count,t.coefficient,d.status as drugStatus,t.coefficient_name from his_drug_warehousing_detail t\n" +
            "inner join his_drug d on t.drug_id=d.id\n" +
            "left join his_drug_warehousing dw on dw.ware_housing_no=t.ware_housing_no\n" +
            "where t.pharmacy_id=#{drug.pharmacyId} and dw.status=3 and t.left_count > 0 \n" +
            "<if test=\"drug.drugType!=null\">\n" +
            "\tand d.drug_type=#{drug.drugType}\n" +
            "</if></script>")
    List<DrugWareHousingDetailDto> findAll(@Param("drug") DrugTypeQueryCriteria criteria);

    @Select("<script>select t.*,d.drug_type,d.moth_use,d.min_moth_use from his_drug_warehousing_detail t\n" +
            "left join his_drug d on t.drug_id=d.id\n" +
            "where t.ware_housing_no=#{wareHousingNo}</script>")
    List<DrugWareHousingDetailVO> queryList(@Param("wareHousingNo")String wareHousingNo);

    @Select("<script>select t.id,drug_id,t.drug_name,scheduled_quantity,t.stock,t.min_stock,t.sale_price ,t.cost_price,term_of_validity,batch_no,t.ware_housing_no,t.drug_spec,t.package_unit,\n" +
            "t.dose_unit,t.pharmacy_id ,t.left_count,t.min_left_count,t.self_price,t.red_pun,equivalent,t.coefficient_name,t.coefficient,t.commodity_price,t.commodity_self_price,\n" +
            "actual_quantity,d.drug_type,IFNULL(d.total_dose,1) as total_dose, dw.supplier_name as drugManufactor from his_drug_warehousing_detail t \n" +
            "            left join his_drug_warehousing dw on t.ware_housing_no=dw.ware_housing_no\n" +
            "            left join his_drug d on t.drug_id=d.id\n" +
            "            where 1=1 and t.pharmacy_id=#{drug.pharmacyId}  and dw.status=3 and t.left_count>0\n" +
            "\tand t.term_of_validity &lt; CURDATE() \n" +
            "\t<if test=\"drug.drugType != null and drug.drugType != ''\">\n" +
            "\t and d.drug_type=#{drug.drugType}\n" +
            "\t</if>\n" +
            "\t<if test=\"drug.drugType == null or drug.drugType == ''\">\n" +
            "\tand d.drug_type in (3,4,5,6)\n" +
            "\t</if>\n" +
            "\t\n</script>")
    List<DrugWareHousingDetailDto> getValidityDrugDetailList(@Param("drug") ValidityDrugCriteria criteria);

    @Select("<script>select t.* from his_drug_warehousing_detail t left join his_drug_warehousing w on t.ware_housing_no=w.ware_housing_no where t.drug_id=#{drugId} and " +
            " (t.term_of_validity is null or  TO_DAYS(t.term_of_validity) > TO_DAYS(NOW())) and t.left_count>0 and w.status=3 order by t.term_of_validity asc </script>")
    List<DrugWareHousingDetail> queryComfireList(@Param("drugId") Long id);
}
